package net.ojbk.study.twobox.web.exceptionResolver;

import com.alibaba.fastjson.support.spring.FastJsonJsonView;
import org.apache.log4j.Logger;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: twobox
 * Date: 2019/4/9
 * Time: 16:52
 * Description: No Description
 */
public class CenterExceptionResolver implements HandlerExceptionResolver {
    private static Logger logger = Logger.getLogger(CenterExceptionResolver.class);

    @Override
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
        ModelAndView mv = new ModelAndView();
        Map<String, Object> attributes = new HashMap<>();
        attributes.put("msg", ex.getMessage());

        FastJsonJsonView view = new FastJsonJsonView();
        view.setAttributesMap(attributes);
        mv.setView(view);
        mv.setStatus(HttpStatus.BAD_REQUEST);
        logger.debug("异常：" + ex.getMessage(), ex);
        return mv;
    }
}
